package cn.ibdsr.human.manager;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author: lvyou
 * @description: Oss文件工具
 * @date: 2020-07-08
 * @lastModified:
 *
 * Date           Author               Description
 * ------------------------------------------------------
 * 2020-07-08     lvyou                初始化
 */
@Component
public class OssFileManager {

    /**
     * 空字符串
     */
    private static final String NULL_STR = "null";

    /**图片访问的前缀*/
    @Value("${guns.fastDfs-url.visit}")
    public String PREFIX_IMAGE_URL;

    /**
     * 剪裁url前缀
     *
     * @param targetImageUrl
     * @return
     */
    public String cutFileUrl(String targetImageUrl) {

        if (StringUtils.isEmpty(targetImageUrl)) {
            return targetImageUrl;
        }
        int imageIndex = targetImageUrl.indexOf(PREFIX_IMAGE_URL);
        if (imageIndex < 0) {
            return targetImageUrl;
        }
        return targetImageUrl.substring(imageIndex + PREFIX_IMAGE_URL.length());
    }

    /**
     * 拼接url前缀
     *
     * @param targetImageUrl
     * @return
     */
    public String setFileUrl(String targetImageUrl) {

        if (StringUtils.isEmpty(targetImageUrl) || NULL_STR.equals(targetImageUrl)) {
            return null;
        }
        int imageIndex = targetImageUrl.indexOf(PREFIX_IMAGE_URL);
        if (imageIndex > -1) {
            return targetImageUrl;
        }
        return PREFIX_IMAGE_URL + targetImageUrl;
    }


}
